using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_ProductEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        Product product;

        if (Int32.TryParse(Request["id"], out id))
            product = Product.Load(id);
        else
            product = null;

        if (product != null)
        {
            if (!string.IsNullOrEmpty(product.ProductName))
				tbxProductName.Text = product.ProductName;
			if (!string.IsNullOrEmpty(product.ProductDefaultImageUrl))
				tbxProductDefaultImageUrl.Text = product.ProductDefaultImageUrl;
			tbxProductPrice.Text = product.ProductPrice.ToString();
			tbxProductBrandId.Text = product.ProductBrandId.ToString();
			tbxProductCategoryId.Text = product.ProductCategoryId.ToString();
			tbxMainProductId.Text = product.MainProductId.ToString();
			if (!string.IsNullOrEmpty(product.DifferBy))
				tbxDifferBy.Text = product.DifferBy;
			cbxIsProductInSale.Checked = product.IsProductInSale;
			tbxSaleTimePrice.Text = product.SaleTimePrice.ToString();
			tbxRating.Text = product.Rating.ToString();
			tbxTotalRater.Text = product.TotalRater.ToString();
			cbxIsActive.Checked = product.IsActive;
			cbxIsFeatured.Checked = product.IsFeatured;
			cbxIsNewArrival.Checked = product.IsNewArrival;
			cbxIsBestSeller.Checked = product.IsBestSeller;
			tbxProductLength.Text = product.ProductLength.ToString();
			tbxProductWidth.Text = product.ProductWidth.ToString();
			tbxProductHeight.Text = product.ProductHeight.ToString();
			tbxProductWieght.Text = product.ProductWieght.ToString();
			if (!string.IsNullOrEmpty(product.ProductSizeUnit))
				tbxProductSizeUnit.Text = product.ProductSizeUnit;
			if (!string.IsNullOrEmpty(product.ProductWeightUnit))
				tbxProductWeightUnit.Text = product.ProductWeightUnit;
			tbxItemInStock.Text = product.ItemInStock.ToString();
			if (!string.IsNullOrEmpty(product.SortToken))
				tbxSortToken.Text = product.SortToken;

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string productName = Security.BindStringInput(tbxProductName.Text, _securityLevel);
		string productDefaultImageUrl = Security.BindStringInput(tbxProductDefaultImageUrl.Text, _securityLevel);
		double productPrice = Security.BindDoubleInput(tbxProductPrice.Text, _securityLevel);
		int productBrandId = Security.BindIntegerInput(tbxProductBrandId.Text, _securityLevel);
		int productCategoryId = Security.BindIntegerInput(tbxProductCategoryId.Text, _securityLevel);
		int mainProductId = Security.BindIntegerInput(tbxMainProductId.Text, _securityLevel);
		string differBy = Security.BindStringInput(tbxDifferBy.Text, _securityLevel);
		bool isProductInSale = cbxIsProductInSale.Checked;
		double saleTimePrice = Security.BindDoubleInput(tbxSaleTimePrice.Text, _securityLevel);
		double rating = Security.BindDoubleInput(tbxRating.Text, _securityLevel);
		int totalRater = Security.BindIntegerInput(tbxTotalRater.Text, _securityLevel);
		bool isActive = cbxIsActive.Checked;
		bool isFeatured = cbxIsFeatured.Checked;
		bool isNewArrival = cbxIsNewArrival.Checked;
		bool isBestSeller = cbxIsBestSeller.Checked;
		double productLength = Security.BindDoubleInput(tbxProductLength.Text, _securityLevel);
		double productWidth = Security.BindDoubleInput(tbxProductWidth.Text, _securityLevel);
		double productHeight = Security.BindDoubleInput(tbxProductHeight.Text, _securityLevel);
		double productWieght = Security.BindDoubleInput(tbxProductWieght.Text, _securityLevel);
		string productSizeUnit = Security.BindStringInput(tbxProductSizeUnit.Text, _securityLevel);
		string productWeightUnit = Security.BindStringInput(tbxProductWeightUnit.Text, _securityLevel);
		int itemInStock = Security.BindIntegerInput(tbxItemInStock.Text, _securityLevel);
		string sortToken = Security.BindStringInput(tbxSortToken.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (Product.Save(
				id, 
				productName,
				productDefaultImageUrl,
				productPrice,
				productBrandId,
				productCategoryId,
				mainProductId,
				differBy,
				isProductInSale,
				saleTimePrice,
				rating,
				totalRater,
				isActive,
				isFeatured,
				isNewArrival,
				isBestSeller,
				productLength,
				productWidth,
				productHeight,
				productWieght,
				productSizeUnit,
				productWeightUnit,
				itemInStock,
				sortToken
                ))
                    Response.Redirect("productlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            Product product = Product.Create(
				productName,
				productDefaultImageUrl,
				productPrice,
				productBrandId,
				productCategoryId,
				mainProductId,
				differBy,
				isProductInSale,
				saleTimePrice,
				rating,
				totalRater,
				isActive,
				isFeatured,
				isNewArrival,
				isBestSeller,
				productLength,
				productWidth,
				productHeight,
				productWieght,
				productSizeUnit,
				productWeightUnit,
				itemInStock,
				sortToken
                );

            if (product != null)
                Response.Redirect("productlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("productlist.aspx");
    }
}
